<?php
// 提交添加宠物信息
// header("content-type:text/html;charset=utf-8");
include_once("conn.php");
include_once("u_getLoginInfo.php");
if($status != 1){
    die("<script>alert('账户处于待注销状态，不可操作！');history.go(-1);</script>");
}
//获得宠物ID
function getID(int $length = 7) {
    $characters = '0123456789';
    $randomString = '';

    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }

    return $randomString;
}

$select_petID = "SELECT pet_ID FROM pet_infos";
$select_hispetID = "SELECT pet_ID FROM history_pet_infos";
$result_petid = mysqli_query($connect_DB,$select_petID);
$result_hispetid = mysqli_query($connect_DB,$select_hispetID);
if($result_petid && $result_hispetid){
    $petIDs = array();
    while ($row = mysqli_fetch_assoc($result_petid)) {
        $petIDs[] = $row['pet_ID'];
    }
    while ($row = mysqli_fetch_assoc($result_hispetid)) {
        $petIDs[] = $row['pet_ID'];
    }
}else{
    die("<script>alert('对比系统宠物ID失败！');history.go(-1);</script>");
}

$petID = "pet".getID();

while(true){
    if(in_array($petID, $petIDs)){
        $petID = "pet".getID();
    }else{
        break;
    }
}

// 设置时区为中国
date_default_timezone_set('Asia/Shanghai');
// 获取当前时间
$now_Date = date('Y-m-d');

$name = $_POST['petName'];
$type = $_POST['petType'];
$birthday = $_POST['petBirthday'];
if ($birthday === '') {
    $birthday = '未填写';
}
$photo = $_FILES['petPhoto'];
if ($photo['name'] === '') {
    if ($type != 'other') {
        $photoName = "default_pet_photo/" . $type . ".jpg";
    } else {
        $type = $_POST['inputPetType'];
        $photoName = "default_pet_photo/其他.jpg";
    }
    $insert_addinfo = "INSERT INTO pet_infos (pet_ID, pet_Name, pet_Type, pet_Birthday, pet_PhotoAddress, pet_AddDate, user_ID)
        VALUES ('" . $petID . "', '" . $name . "', '" . $type . "', '" . $birthday . "', '" . $photoName . "', '" . $now_Date . "', '" . $userID . "')";
    if (mysqli_query($connect_DB, $insert_addinfo)) {
        echo "<script>alert('添加成功！');location='user_myPetInfo.php';</script>";
    } else {
        die("<script>alert('写入宠物信息失败！请重试！');location='user_addPetInfo.php';</script>");
    }
}

if ($photo['name'] != '') {
    if($type == 'other'){
        $type = $_POST['inputPetType'];
    }
    if (!in_array($photo['type'], ['image/jpg', 'image/jpeg', 'image/png'])) {
        die("<script>alert('图片格式错误！请重新选择！');location='user_addPetInfo.php';</script>");
    }
    if ($photo['size'] > 2000000) {
        die("<script>alert('图片大小错误！大小不能超过2M！请重新选择！');location='user_addPetInfo.php';</script>");
    }
    // 图片上传路径
    $uploadAddress = "../public/pic/pet_photos/" . $userID . "/";
    // 图片名称
    $photoName = $petID . "." . pathinfo($photo['name'], PATHINFO_EXTENSION);
    // 完整路径
    $upload = $uploadAddress . $photoName;
    // 检查文件是否已经存在,有则删除
    if (file_exists($upload)) {
        unlink($upload);
    }
    // 创建文件夹（如果不存在）
    if (!file_exists($uploadAddress)) {
        mkdir($uploadAddress, 0777, true);
    }
    if (move_uploaded_file($photo['tmp_name'], $upload)) {
        $insert_addinfo = "INSERT INTO pet_infos (pet_ID, pet_Name, pet_Type, pet_Birthday, pet_PhotoAddress, pet_AddDate, user_ID)
        VALUES ('" . $petID . "', '" . $name . "', '" . $type . "', '" . $birthday . "', '" . $photoName . "', '" . $now_Date . "', '" . $userID . "')";
        if (mysqli_query($connect_DB, $insert_addinfo)) {
            echo "<script>alert('添加宠物信息成功！');location='user_myPetInfo.php';</script>";
        } else {
            unlink($upload);
            die("<script>alert('添加宠物信息失败！请重试！');location='user_addPetInfo.php'</script>");
        }
    } else {
        die("<script>alert('图片上传失败！');location='user_addPetInfo.php';</script>");
    }
}


$connect_DB->close();
?>